import { AtButton, AtInput, AtNavBar } from 'taro-ui';
import React from 'react';
import Taro from '@tarojs/taro'
import config from '../../config.json'
import { View } from '@tarojs/components';

class enterInvitedCode extends React.Component<any, any>{
    constructor(...props) {
        super(props);
        this.state = {
            code: ''
        }
    }
    handleChange(value, evt) {
        let obj = {};
        obj[value] = evt;
        this.setState(obj);
        return value;
    }
    onSubmit() {
        Taro.request({
            url: config.domainName + '/user/authority/update',
            method: "POST",
            data: {
                code: this.state.code,
                authority: 2,
            },
            header: {
                token: Taro.getStorageSync('token'),
            },
            success(res) {
                if (res.data.code === 0) {
                    if (res) {
                        Taro.showToast({
                            title: '您已成为管理员',
                            icon: "success"
                        })
                        Taro.navigateBack({
                            delta: 2,
                        })
                    } else {
                        Taro.showToast({
                            title: '输入有误',
                            icon: "error" as any
                        })
                    }
                } else {
                    Taro.showToast({
                        title: '服务器异常',
                        icon: "error" as any,
                    })
                }
            }
        })
    }
    render() {
        return (
            <>
                <AtNavBar
                    onClickLeftIcon={() => { Taro.navigateBack() }}
                    color='#000'
                    title='邀请码'
                    leftIconType="chevron-left"
                />
                <View style={{position: "relative", top: "5rem"}}>
                    <AtInput
                        title="邀请码:"
                        name="code"
                        value={this.state.code}
                        onChange={this.handleChange.bind(this, 'code')}
                    />
                    <View style={{ margin: "20px 10px" }}>
                        <AtButton type="primary" onClick={this.onSubmit.bind(this)}>提交</AtButton>
                    </View>
                </View>
            </>

        )
    }
}
export default enterInvitedCode;